/* begin:QFL */
(function() {

var _w = window, _d = document,
_NAME = 'QFL',
_$ = function(s) {
  return _d.getElementById(s);
},
_add = function(mname, obj) {
  _w[_NAME][mname] = obj;
};

_w[_NAME] = {};

_add('dom', {
  $ : _$
});

_add('event', {
  on : function () {
    if (_w.addEventListener) {
      return function(el, type, fn, capture) {
        el = typeof el == 'string' ? _$(el) : el;
        el.addEventListener(type, fn, (!!capture));
      };
    } else if (_w.attachEvent) {
      return function(el, type, fn) {
        el = typeof el == 'string' ? _$(el) : el;
        el.attachEvent("on" + type, fn);
      };
    } else {
      return function(){};
    }
  }(),
  off : function() {
    if (_w.removeEventListener) {
      return function (el, type, fn, capture) {
        el = typeof el == 'string' ? _$(el) : el;
        el.removeEventListener(type, fn, !!capture);
      };
    } else if (_w.detachEvent) {
      return function (el, type, fn) {
        el = typeof el == 'string' ? _$(el) : el;
        el.detachEvent("on" + type, fn);
      };
    } else {
      return function(){};
    }
  }()
});

_add('io', {

  ajax : (function() {
    var Ajax = function(url, obj) {
      var newO, xh, cb;
      if (typeof obj == 'function') {
        newO = arguments[2] || {};
        newO.onSuccess = obj;
        obj = newO;
      }

      xh = this.getTransport();

      cb = function () {
        if(xh.readyState == 4 && xh.status == 200) {
          obj.onSuccess(xh.responseText);
        }
        xh.onreadystatechange = null;
      };
      if (obj.onSuccess) {
        xh.onreadystatechange = cb;
      }
      xh.open("GET", url, true);
      xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      //xmlhttp.setRequestHeader("Content-Type", "text/plain");
      xh.send(null);
      xh = null;
      cb = null;
    };

    Ajax.prototype.getTransport = function() {
      var i, j, t, transports = [
        function() {return new XMLHttpRequest();},
        function() {return new ActiveXObject('Msxml2.XMLHTTP');},
        function() {return new ActiveXObject('Microsoft.XMLHTTP');}
      ];
      for (i=0,j=transports.length;i<j;i++) {
        try {
          t = transports[i]();
          if (t) {
            Ajax.prototype.getTransport = transports[i];
            return t;
          }
        } catch (e) {}
      }
    };

    return Ajax;
  }())

});

_add('anim', (function() {
  var Anim = function(el, attr, ftobj) {
    if (typeof ftobj != 'object') {
      ftobj = {to : ftobj};
    }
    this.el = typeof el == 'string' ? _$(el) : el;
    this.attr = attr;
    this.from = 'from' in ftobj ? ftobj.from : false;
    this.to = ftobj.to;
    this.lasttime = 'lasttime' in ftobj ? ftobj.lasttime : 300;
    this.interval = 'interval' in ftobj ? ftobj.interval : 15;
    this.max = 'max' in ftobj ? ftobj.max : this.lasttime * 2 / this.interval;
    this.paused = false;
    this.iid = null;
    this.running = false;
  };
  Anim.prototype = {
    run : function() {
      if (this.running) {
        return;
      }
      this.running = true;

      var el = this.el,
        at = this.attr, oldF;
      if (!this.paused) {
        oldF = this.from;
        this.from = oldF === false ? el[at] : oldF;
        this.piece = (this.to - this.from) * (this.interval+1) / this.lasttime;
        el[at] = this.from + this.piece;
        this.from = oldF;
      }
      if (this.piece === 0) {
        this.running = false;
        return;
      }
      this.iid = this._begin();
    },
    _begin : function() {
      var that = this, fn = function() {
        var oldV = that.el[that.attr];
        if (that.count++ >= that.max || oldV == that.to) {
          that._stop();
          return;
        }
        that.el[that.attr] = oldV + that.piece;
      };
      this.count = 0;
      return setInterval(fn, that.interval);
    },
    pause : function() {
      this.paused = true;
      clearInterval(this.iid);
    },
    _stop : function() {
      this.running = false;
      clearInterval(this.iid);
    }
  };
  return Anim;
}()));

}());
/* end:QFL */

